<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis%E5%BB%B6%E8%BF%9F%E9%97%AE%E9%A2%98%E8%AF%8A%E6%96%AD/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">5238 字 ~25分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<h1 id="diagnosing-latency-issues">Diagnosing latency issues</h1>

<blockquote>
<p>诊断延迟问题</p>

<p>源文地址：<a href="https://redis.io/docs/reference/optimization/latency/">https://redis.io/docs/reference/optimization/latency/</a></p>
</blockquote>

<p>Finding the causes of slow responses</p>

<blockquote>
<p>寻找响应慢的原因。</p>
</blockquote>

<p>This document will help you understand what the problem could be if you are experiencing latency problems with Redis.</p>

<blockquote>
<p>这个文档将帮你理解Redis延迟问题的原因。</p>
</blockquote>

<p>In this context <em>latency</em> is the maximum delay between the time a client issues a command and the time the reply to the command is received by the client. Usually Redis processing time is extremely low, in the sub microsecond range, but there are certain conditions leading to higher latency figures.</p>

<p>这篇文章里的*延迟*指的是客户端发送命令到客户端接收返回之间的最大延迟。通常Redis处理特别快，在微秒级别，但是有些条件会导致高延迟。</p>

<h2 id="i-ve-little-time-give-me-the-checklist">I&rsquo;ve little time, give me the checklist</h2>

<blockquote>
<p>紧急情况检查清单</p>
</blockquote>

<p>The following documentation is very important in order to run Redis in a low latency fashion. However I understand that we are busy people, so let&rsquo;s start with a quick checklist. If you fail following these steps, please return here to read the full documentation.</p>

<blockquote>
<p>下面的文档对于低延迟运行redis很重要，但是，我们都是大忙人，所以咱先来一个快速检查清单。如果你按以下步骤没成功，再来读整篇文档</p>
</blockquote>

<ol>
<li>Make sure you are not running slow commands that are blocking the server. Use the Redis <a href="https://redis.io/commands/slowlog">Slow Log feature</a> to check this.</li>
</ol>

<blockquote>
<p>确保你没有运行阻塞服务的慢命令。使用SlowLog功能可以检查。</p>
</blockquote>

<ol>
<li>For EC2 users, make sure you use HVM based modern EC2 instances, like m3.medium. Otherwise fork() is too slow.</li>
</ol>

<blockquote>
<p>对于EC2（Elastic Compute Cloud 亚马逊弹性计算云）用户，确保你使用基于现代EC2实例的HVM，如 m3.medium。否则fork()操作太慢了。</p>
</blockquote>

<ol>
<li>Transparent huge pages must be disabled from your kernel. Use <code>echo never &gt; /sys/kernel/mm/transparent_hugepage/enabled</code> to disable them, and restart your Redis process.</li>
</ol>

<blockquote>
<p>THP必须被禁用，使用 <code>echo never &gt; /sys/kernel/mm/transparent_hugepage/enabled</code>禁用然后重启Redis进程。</p>
</blockquote>

<ol>
<li>If you are using a virtual machine, it is possible that you have an intrinsic latency that has nothing to do with Redis. Check the minimum latency you can expect from your runtime environment using <code>./redis-cli --intrinsic-latency 100</code>. Note: you need to run this command in <em>the server</em> not in the client.</li>
</ol>

<blockquote>
<p>如果用虚拟机，内部的延迟Redis没法处理。你可以使用<code>./redis-cli --intrinsic-latency 100</code>命令从运行环境获取最低延迟信息。注意：你需要在服务器端运行这个命令，而不是客户端。</p>
</blockquote>

<ol>
<li>Enable and use the <a href="https://redis.io/topics/latency-monitor">Latency monitor</a> feature of Redis in order to get a human readable description of the latency events and causes in your Redis instance.
&gt; 启用<a href="https://redis.io/topics/latency-monitor">Latency monitor</a>功能来获取导致你Redis实例延迟的事件的描述。</li>
</ol>

<p>In general, use the following table for durability VS latency/performance tradeoffs, ordered from stronger safety to better latency.</p>

<blockquote>
<p>基本上，使用以下表格持久化延迟性能权衡策略，按安全性到低延迟进行排序</p>
</blockquote>

<ol>
<li>AOF + fsync always: this is very slow, you should use it only if you know what you are doing.
&gt;  AOF + fsync always: 非常慢</li>
<li>AOF + fsync every second: this is a good compromise.
&gt; AOF + fsync every second 比较折中</li>
<li>AOF + fsync every second + no-appendfsync-on-rewrite option set to yes: this is as the above, but avoids to fsync during rewrites to lower the disk pressure.
&gt; AOF + fsync every second + no-appendfsync-on-rewrite option set to yes: 与上面相同，但避免了重写时进行fsync，以降低磁盘压力。</li>
<li>AOF + fsync never. Fsyncing is up to the kernel in this setup, even less disk pressure and risk of latency spikes.
&gt; AOF + fsync never:更低的磁盘负载和延迟毛刺风险。</li>
<li>RDB. Here you have a vast spectrum of tradeoffs depending on the save triggers you configure.
&gt; RDB.基于配置的触发器性能差距很大</li>
</ol>

<p>And now for people with 15 minutes to spend, the details&hellip;
&gt; 接着说明要花15分钟来看详细说明。</p>

<h2 id="measuring-latency">Measuring latency</h2>

<blockquote>
<p>测量延迟</p>
</blockquote>

<p>If you are experiencing latency problems, you probably know how to measure it in the context of your application, or maybe your latency problem is very evident even macroscopically. However redis-cli can be used to measure the latency of a Redis server in milliseconds, just try:</p>

<blockquote>
<p>如果你遇到延迟问题，你也许知道如何在你的应用上下文中测量延迟，或者你的延迟问题非常明显甚至肉眼可见。<code>redis-cli</code>可以被用来测量 Redis 服务的毫秒单位下的延迟。使用如下命令：</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">redis-cli --latency -h <span style="color:#4e9a06">`</span>host<span style="color:#4e9a06">`</span> -p <span style="color:#4e9a06">`</span>port<span style="color:#4e9a06">`</span></code></pre></div>
<h2 id="using-the-internal-redis-latency-monitoring-subsystem">Using the internal Redis latency monitoring subsystem</h2>

<blockquote>
<p>使用内置的Redis延迟监控子系统</p>
</blockquote>

<p>Since Redis 2.8.13, Redis provides latency monitoring capabilities that are able to sample different execution paths to understand where the server is blocking. This makes debugging of the problems illustrated in this documentation much simpler, so we suggest enabling latency monitoring ASAP. Please refer to the <a href="https://redis.io/topics/latency-monitor">Latency monitor documentation</a>.</p>

<blockquote>
<p>Redis2.8.13以后，Redis提供了延迟监控单元，来对不同的执行路径进行抽样，来标记服务哪里阻塞。这使得调试本文中的问题更简单，所以我们建议尽快启动延迟监控。参考文档 <a href="https://redis.io/topics/latency-monitor">Latency monitor documentation</a>.</p>
</blockquote>

<p>While the latency monitoring sampling and reporting capabilities will make it simpler to understand the source of latency in your Redis system, it is still advised that you read this documentation extensively to better understand the topic of Redis and latency spikes.</p>

<blockquote>
<p>延迟监控采样和报告能力可以帮助我们理解自己Redis系统延迟的根源，在这里仍然建议您广泛阅读本文档，以更好地了解 Redis 和延迟峰值的主题。</p>
</blockquote>

<h2 id="latency-baseline">Latency baseline</h2>

<blockquote>
<p>延迟基准线</p>
</blockquote>

<p>There is a kind of latency that is inherently part of the environment where you run Redis, that is the latency provided by your operating system kernel and, if you are using virtualization, by the hypervisor you are using.</p>

<blockquote>
<p>有一种延迟是Redis运行环境内生的延迟，是由你的操作系统内核产生的，并且如果你使用虚拟化，会由虚拟机管理程序产生。</p>
</blockquote>

<p>While this latency can&rsquo;t be removed it is important to study it because it is the baseline, or in other words, you won&rsquo;t be able to achieve a Redis latency that is better than the latency that every process running in your environment will experience because of the kernel or hypervisor implementation or setup.</p>

<blockquote>
<p>虽然无法消除此延迟，但研究它很重要，因为它是基线，换句话说，环境中运行的每个进程由于内核或虚拟机管理程序实现或设置产生的Redis延迟就已经到底了，你再怎么优化也不可能优化的比这个延迟更低。</p>
</blockquote>

<p>We call this kind of latency <strong>intrinsic latency</strong>, and <code>redis-cli</code> starting from Redis version 2.8.7 is able to measure it. This is an example run under Linux 3.11.0 running on an entry level server.</p>

<blockquote>
<p>我们称这种延迟为内源性延迟，在Redis 2.8.7版本开始<code>redis-cli</code>工具可以测试它。下面以Linux 3.11.0版本的入门级服务器上运行为例。</p>
</blockquote>

<p>Note: the argument <code>100</code> is the number of seconds the test will be executed. The more time we run the test, the more likely we&rsquo;ll be able to spot latency spikes. 100 seconds is usually appropriate, however you may want to perform a few runs at different times. Please note that the test is CPU intensive and will likely saturate a single core in your system.</p>

<blockquote>
<p>注意：参数100是测试执行将被执行的秒数。我们运行测试的时间越长，我们越能定位延迟峰值。100秒一般就很合适，但你可能希望不同的时间执行一些次测试运行。要注意这个测试会占用大量CPU资源，而且可能会使系统中的单个核饱和。</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-yaml" data-lang="yaml">$<span style="color:#f8f8f8;text-decoration:underline"> </span>./redis-cli<span style="color:#f8f8f8;text-decoration:underline"> </span>--intrinsic-latency<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">100</span><span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">16</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">50</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">53</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">83</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">115</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.</code></pre></div>
<p>Note: redis-cli in this special case needs to <strong>run in the server</strong> where you run or plan to run Redis, not in the client. In this special mode redis-cli does not connect to a Redis server at all: it will just try to measure the largest time the kernel does not provide CPU time to run to the redis-cli process itself.</p>

<blockquote>
<p>注意，<code>redis-cli</code>在这个特殊场景中需要在你运行或计划运行Redis实例的<strong>服务器上运行</strong>，而不是在其他客户端服务器上运行。在这种特殊模式下，<code>redis-cli</code>工具压根不会连接Redis服务：它只会尝试测量 内核从不提供CPU时间运行 到 切换到Redis-cli 进程本身的最长时间。</p>
</blockquote>

<p>In the above example, the intrinsic latency of the system is just 0.115 milliseconds (or 115 microseconds), which is a good news, however keep in mind that the intrinsic latency may change over time depending on the load of the system.</p>

<blockquote>
<p>在上面的例子中，系统内生延迟只有0.115毫秒（或115微秒），很好。然而要记住内生延迟会随着系统负载的变化而改变。</p>
</blockquote>

<p>Virtualized environments will not show so good numbers, especially with high load or if there are noisy neighbors. The following is a run on a Linode 4096 instance running Redis and Apache:</p>

<blockquote>
<p>虚拟化环境的数值不会这么好，特别是在高负载下，或者是同宿主的其他虚拟机负载高的情况。以下是Linode 4096实例上执行运行 Redis 和 Apache的测试：</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-yaml" data-lang="yaml">$<span style="color:#f8f8f8;text-decoration:underline"> </span>./redis-cli<span style="color:#f8f8f8;text-decoration:underline"> </span>--intrinsic-latency<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">100</span><span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">573</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">695</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">919</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1606</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">3191</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">9243</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Max latency so far</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">9671</span><span style="color:#f8f8f8;text-decoration:underline"> </span>microseconds.</code></pre></div>
<p>Here we have an intrinsic latency of 9.7 milliseconds: this means that we can&rsquo;t ask better than that to Redis. However other runs at different times in different virtualization environments with higher load or with noisy neighbors can easily show even worse values. We were able to measure up to 40 milliseconds in systems otherwise apparently running normally.</p>

<blockquote>
<p>可以看到内生 延迟达到了9.7毫秒，这意味着我们请求Redis不会比这个更快了。然而其他的在不同时间在不同的高负载虚拟化环境或高负载集群的场景下，内生延迟的值会更加糟糕。我们能够在显然正常运行的系统中测量高达40毫秒的差距。</p>
</blockquote>

<h2 id="latency-induced-by-network-and-communication">Latency induced by network and communication</h2>

<blockquote>
<p>网络和通信产生的延迟</p>
</blockquote>

<p>Clients connect to Redis using a TCP/IP connection or a Unix domain connection. The typical latency of a 1 Gbit/s network is about 200 us, while the latency with a Unix domain socket can be as low as 30 us. It actually depends on your network and system hardware. On top of the communication itself, the system adds some more latency (due to thread scheduling, CPU caches, NUMA placement, etc &hellip;). System induced latencies are significantly higher on a virtualized environment than on a physical machine.</p>

<p>客户端使用TCP/IP连接或Unix域连接来连接到Redis服务。千兆网络的常规延迟大概200微秒，使用Unix域套接字可以达到低到30微秒。延迟取决于你的网络和系统硬件。除了通信本身之外，系统还增加了一些延迟（由于线程调度，CPU缓存，NUMA放置等原因）。虚拟化环境中的系统引起的延迟明显高于物理机。</p>

<p>The consequence is even if Redis processes most commands in sub microsecond range, a client performing many roundtrips to the server will have to pay for these network and system related latencies.</p>

<p>结果就是，即使Redis大部分命令处理时间在亚毫秒级别范围内，客户端到服务器端执行往返也会因为网络和系统的消耗而造成延迟。</p>

<p>An efficient client will therefore try to limit the number of roundtrips by pipelining several commands together. This is fully supported by the servers and most clients. Aggregated commands like MSET/MGET can be also used for that purpose. Starting with Redis 2.4, a number of commands also support variadic parameters for all data types.</p>

<p>一个高效的客户端会因为使用管道指执行一些命令来限制到服务端的往返次数。这在服务端和多数客户端都是支持的。聚合命令如 MSET/MGET也可以用来达到这个目的。从Redis2.4开始，许多命令还支持所有数据类型的可变参数（即一次可以传多个参数）。</p>

<p>Here are some guidelines:</p>

<blockquote>
<p>这有一些指导方针：</p>
</blockquote>

<ul>
<li>If you can afford it, prefer a physical machine over a VM to host the server.
&gt; 有能力上物理机，别用虚拟机。</li>
<li>Do not systematically connect/disconnect to the server (especially true for web based applications). Keep your connections as long lived as possible.
&gt; 不要系统地连接/断开与服务器的连接（特别是对于基于Web的应用程序）。尽可能使用长连接。</li>
<li>If your client is on the same host than the server, use Unix domain sockets.
&gt; 如果您的客户端与服务器位于同一主机上，请使用 Unix 域套接字</li>
<li>Prefer to use aggregated commands (MSET/MGET), or commands with variadic parameters (if possible) over pipelining.
&gt; 首选使用聚合命令 （MSET/MGET）或带有可变参数的命令（如果可能）而不是流水线。</li>
<li>Prefer to use pipelining (if possible) over sequence of roundtrips.
&gt; 首选使用流水线（如果可能）而不是往返顺序。</li>
<li>Redis supports Lua server-side scripting to cover cases that are not suitable for raw pipelining (for instance when the result of a command is an input for the following commands).
&gt; Redis 支持 Lua 服务器端脚本，以涵盖不适合原始管道的情况（例如，当命令的结果是后续命令的输入参数时）。</li>
</ul>

<p>On Linux, some people can achieve better latencies by playing with process placement (taskset), cgroups, real-time priorities (chrt), NUMA configuration (numactl), or by using a low-latency kernel. Please note vanilla Redis is not really suitable to be bound on a <strong>single</strong> CPU core. Redis can fork background tasks that can be extremely CPU consuming like <a href="https://redis.io/commands/bgsave"><code>BGSAVE</code></a> or <a href="https://redis.io/commands/bgrewriteaof"><code>BGREWRITEAOF</code></a>. These tasks must <strong>never</strong> run on the same core as the main event loop.</p>

<blockquote>
<p>在 Linux 上，有些人可以通过使用进程放置（任务集）、cgroups、实时优先级 （chrt）、NUMA 配置（numactl）或使用低延迟内核来实现更好的延迟。请注意，普通的 Redis 并不适合绑定在 <strong>单个</strong> CPU 内核上。Redis 可以fork后台任务，这些任务可能非常消耗 CPU，例如 [<code>BGSAVE</code>]（<a href="https://redis.io/commands/bgsave）或">https://redis.io/commands/bgsave）或</a> [<code>BGREWRITEAOF</code>]（<a href="https://redis.io/commands/bgrewriteaof）">https://redis.io/commands/bgrewriteaof）</a> 。这些任务<strong>决不能</strong>在与主事件循环相同的内核上运行。</p>
</blockquote>

<p>In most situations, these kind of system level optimizations are not needed. Only do them if you require them, and if you are familiar with them.</p>

<blockquote>
<p>在大多数场景 ，这些系统级别优化并不必要。只有确实需要并且了解它们的情况下才能做。</p>
</blockquote>

<h2 id="single-threaded-nature-of-redis">Single threaded nature of Redis</h2>

<blockquote>
<p>Redis单线程的本质</p>
</blockquote>

<p>Redis uses a <em>mostly</em> single threaded design. This means that a single process serves all the client requests, using a technique called <strong>multiplexing</strong>. This means that Redis can serve a single request in every given moment, so all the requests are served sequentially. This is very similar to how Node.js works as well. However, both products are not often perceived as being slow. This is caused in part by the small amount of time to complete a single request, but primarily because these products are designed to not block on system calls, such as reading data from or writing data to a socket.</p>

<blockquote>
<p>Redis 使用 <em>大部分</em> 单线程设计。这意味着单个进程使用一种称为<strong>多路复用</strong>的技术为所有客户端请求提供服务。这意味着 Redis 可以在每个给定时刻为单个请求提供服务，因此所有请求都是按顺序处理的。这与 Node.js的工作方式非常相似。然而，这两种产品通常都不被认为是缓慢的。这部分是由于完成单个请求的时间很短，但主要是因为这些产品被设计为不阻塞系统调用，例如从套接字读取数据或将数据写入套接字。</p>
</blockquote>

<p>I said that Redis is <em>mostly</em> single threaded since actually from Redis 2.4 we use threads in Redis in order to perform some slow I/O operations in the background, mainly related to disk I/O, but this does not change the fact that Redis serves all the requests using a single thread.</p>

<blockquote>
<p>我说过 Redis*大部分*是单线程的，因为实际上从Redis 2.4开始，我们在Redis中使用线程，以便在后台执行一些缓慢的I/O操作，主要与磁盘I/O有关，但这并不能改变Redis使用单个线程为所有请求提供服务的事实。</p>
</blockquote>

<h2 id="latency-generated-by-slow-commands">Latency generated by slow commands</h2>

<blockquote>
<p>由慢命令产生的延迟</p>
</blockquote>

<p>A consequence of being single thread is that when a request is slow to serve all the other clients will wait for this request to be served. When executing normal commands, like <a href="https://redis.io/commands/get"><code>GET</code></a> or <a href="https://redis.io/commands/set"><code>SET</code></a> or <a href="https://redis.io/commands/lpush"><code>LPUSH</code></a> this is not a problem at all since these commands are executed in constant (and very small) time. However there are commands operating on many elements, like <a href="https://redis.io/commands/sort"><code>SORT</code></a>, <a href="https://redis.io/commands/lrem"><code>LREM</code></a>, <a href="https://redis.io/commands/sunion"><code>SUNION</code></a> and others. For instance taking the intersection of two big sets can take a considerable amount of time.</p>

<blockquote>
<p>单线程的结果是，当一个请求处理速度慢了，所有其他客户端将等待此请求处理完成。当执行常规命令时，如<a href="https://redis.io/commands/get"><code>GET</code></a>或<a href="https://redis.io/commands/set"><code>SET</code></a>或<a href="https://redis.io/commands/lpush"><code>LPUSH</code></a>，这根本不是问题，因为这些命令是在恒定(和非常小)的时间内执行的。但是，有许多命令对许多元素进行操作，例如<a href="https://redis.io/commands/sort"><code>SORT</code></a>，<a href="https://redis.io/commands/lrem"><code>LREM</code></a>，<a href="https://redis.io/commands/sunion"><code>SUNION</code></a>等。例如，采用两个大集合的交集可能需要相当长的时间。</p>
</blockquote>

<p>The algorithmic complexity of all commands is documented. A good practice is to systematically check it when using commands you are not familiar with.</p>

<blockquote>
<p>所有的算法复杂度都有记录。一个好的实践是，当你使用一个不熟悉的命令时，先系统地检查一下它。</p>
</blockquote>

<p>If you have latency concerns you should either not use slow commands against values composed of many elements, or you should run a replica using Redis replication where you run all your slow queries.</p>

<blockquote>
<p>如果您有延迟问题，那你就不应对由许多元素组成的值使用慢速命令，或者你应该在Redis副本上执行慢查询。</p>
</blockquote>

<p>It is possible to monitor slow commands using the Redis <a href="https://redis.io/commands/slowlog">Slow Log feature</a>.</p>

<blockquote>
<p>使用 <a href="https://redis.io/commands/slowlog">Slow Log工具</a>可以监控慢命令。</p>
</blockquote>

<p>Additionally, you can use your favorite per-process monitoring program (top, htop, prstat, etc &hellip;) to quickly check the CPU consumption of the main Redis process. If it is high while the traffic is not, it is usually a sign that slow commands are used.</p>

<blockquote>
<p>此外，您可以使用自己喜欢的进程监控程序（<code>top</code>，<code>htop</code>，<code>prstat</code>等）来快速检查主Redis进程的CPU消耗。如果它很高而流量不高，则通常表示使用了慢速命令。</p>
</blockquote>

<p><strong>IMPORTANT NOTE</strong>: a VERY common source of latency generated by the execution of slow commands is the use of the <a href="https://redis.io/commands/keys"><code>KEYS</code></a> command in production environments. <a href="https://redis.io/commands/keys"><code>KEYS</code></a>, as documented in the Redis documentation, should only be used for debugging purposes. Since Redis 2.8 a new commands were introduced in order to iterate the key space and other large collections incrementally, please check the <a href="https://redis.io/commands/scan"><code>SCAN</code></a>, <a href="https://redis.io/commands/sscan"><code>SSCAN</code></a>, <a href="https://redis.io/commands/hscan"><code>HSCAN</code></a> and <a href="https://redis.io/commands/zscan"><code>ZSCAN</code></a> commands for more information.</p>

<blockquote>
<p><strong>重点注意</strong>：一个非常常见的因慢命令执行而产生的延迟是在生产环境的使用 <a href="https://redis.io/commands/keys"><code>KEYS</code></a>命令。 <a href="https://redis.io/commands/keys"><code>KEYS</code></a>命令如同在Redis文档中记录的一样，应该只被用于调试目的。Redis2.8后新引入增量迭代key和大集群的命令，具体查看 <a href="https://redis.io/commands/scan"><code>SCAN</code></a>, <a href="https://redis.io/commands/sscan"><code>SSCAN</code></a>, <a href="https://redis.io/commands/hscan"><code>HSCAN</code></a> and <a href="https://redis.io/commands/zscan"><code>ZSCAN</code></a> 命令的信息。</p>
</blockquote>

<h2 id="latency-generated-by-fork">Latency generated by fork</h2>

<blockquote>
<p>因<code>fork</code>产生的延迟</p>
</blockquote>

<p>In order to generate the RDB file in background, or to rewrite the Append Only File if AOF persistence is enabled, Redis has to fork background processes. The fork operation (running in the main thread) can induce latency by itself.</p>

<blockquote>
<p>为了在后台生成RDB文件，或者重写AOF文件，Redis会fork后台进程。在主纯种中进行fork操作会产生延迟。</p>
</blockquote>

<p>Forking is an expensive operation on most Unix-like systems, since it involves copying a good number of objects linked to the process. This is especially true for the page table associated to the virtual memory mechanism</p>

<blockquote>
<p>fork操作在多数Unix系统中是很重的操作，因为会复制主进程中的大量对象。在虚拟机的页表中尤其明显。</p>
</blockquote>

<p>For instance on a Linux/AMD64 system, the memory is divided in 4 kB pages. To convert virtual addresses to physical addresses, each process stores a page table (actually represented as a tree) containing at least a pointer per page of the address space of the process. So a large 24 GB Redis instance requires a page table of 24 GB / 4 kB * 8 = 48 MB.</p>

<blockquote>
<p>例如在 Linux或AMD64系统，内存以4kb的页进行分隔。为了转换虚拟地址到物理地址，每个进程会存储一个页表（实际以树形式展现）包含至少一个进程地址空间的指针。所以一个24G的Redis实例，需要一个48M的页表。</p>
</blockquote>

<p>When a background save is performed, this instance will have to be forked, which will involve allocating and copying 48 MB of memory. It takes time and CPU, especially on virtual machines where allocation and initialization of a large memory chunk can be expensive.</p>

<blockquote>
<p>当一个后台保存执行时，这个实例将被form，同时将分配和复制48M内存。这将会消耗时间和CPU，特别在虚拟机上，分配和初始化一个大内存块消耗很大。</p>
</blockquote>

<h2 id="fork-time-in-different-systems">Fork time in different systems</h2>

<blockquote>
<p>不同系统的Fork时间</p>
</blockquote>

<p>Modern hardware is pretty fast at copying the page table, but Xen is not. The problem with Xen is not virtualization-specific, but Xen-specific. For instance using VMware or Virtual Box does not result into slow fork time. The following is a table that compares fork time for different Redis instance size. Data is obtained performing a BGSAVE and looking at the <code>latest_fork_usec</code> filed in the <a href="https://redis.io/commands/info"><code>INFO</code></a> command output.</p>

<blockquote>
<p>现代硬件复制页表很快，但是Xen虚拟机不是。XEN虚拟机的问题不是虚拟化特性，而是Xen特性。例如使用VMware或者VirtualBox不会减慢fork时间。以下是不同实例数下fork时间对比的表格。数据是运行bgsave后观察<code>INFO</code>命令输出的<code>latest_fork_usec</code>字段取得的。</p>
</blockquote>

<p>However the good news is that <strong>new types of EC2 HVM based instances are much better with fork times</strong>, almost on par with physical servers, so for example using m3.medium (or better) instances will provide good results.</p>

<blockquote>
<p>好消息是<strong>EC2 HVM新类型的实例fork时间很好</strong>，和物理机差不多了，所以使用如m3.medium(或更好的)实例可以提供更好的效果。</p>
</blockquote>

<ul>
<li><strong>Linux beefy VM on VMware</strong> 6.0GB RSS forked in 77 milliseconds (12.8 milliseconds per GB).</li>
<li><strong>Linux running on physical machine (Unknown HW)</strong> 6.1GB RSS forked in 80 milliseconds (13.1 milliseconds per GB)</li>
<li><strong>Linux running on physical machine (Xeon @ 2.27Ghz)</strong> 6.9GB RSS forked into 62 milliseconds (9 milliseconds per GB).</li>
<li><strong>Linux VM on 6sync (KVM)</strong> 360 MB RSS forked in 8.2 milliseconds (23.3 milliseconds per GB).</li>
<li><strong>Linux VM on EC2, old instance types (Xen)</strong> 6.1GB RSS forked in 1460 milliseconds (239.3 milliseconds per GB).</li>
<li><strong>Linux VM on EC2, new instance types (Xen)</strong> 1GB RSS forked in 10 milliseconds (10 milliseconds per GB).</li>
<li><strong>Linux VM on Linode (Xen)</strong> 0.9GBRSS forked into 382 milliseconds (424 milliseconds per GB).</li>
</ul>

<p>类型 | Rss | 时间（毫秒）| 每GB平均值
&mdash;&mdash; | &mdash;- | &ndash; | &ndash;
 <strong>Linux beefy VM on VMware</strong> | 6GB RSS | 77 | 12.8
 <strong>Linux running on physical machine (Unknown HW)</strong> | 6.1GB | 80 | 13.1
 <strong>Linux running on physical machine (Xeon @ 2.27Ghz)</strong> | 6.9GB RSS | 62 | 9
 <strong>Linux VM on 6sync (KVM)</strong> | 360MB | 8.2 | 23.3
 <strong>Linux VM on EC2, old instance types (Xen)</strong> | 6.1GB | 1460 | 239.3
 <strong>Linux VM on EC2, new instance types (Xen)</strong> | 1GB | 10 | 10
 <strong>Linux VM on Linode (Xen)</strong> | 0.9GB | 382 | 424</p>

<p>As you can see certain VMs running on Xen have a performance hit that is between one order to two orders of magnitude. For EC2 users the suggestion is simple: use modern HVM based instances.</p>

<p>如您所见，在 Xen 上运行的某些 VM 的性能下降幅度在一个数量级到两个数量级之间。对于 EC2 用户，建议很简单：使用基于 HVM 的现代实例。</p>

<h2 id="latency-induced-by-transparent-huge-pages">Latency induced by transparent huge pages</h2>

<blockquote>
<p>由 <code>transparent huge pages</code> 透明大页导致的延迟</p>
</blockquote>

<p>Unfortunately when a Linux kernel has transparent huge pages enabled, Redis incurs to a big latency penalty after the <code>fork</code> call is used in order to persist on disk. Huge pages are the cause of the following issue:</p>

<blockquote>
<p>当Linux内容开启了透明大页（transparent huge pages， THP），Redis在为了在硬盘持久化而执行<code>fork</code>后会引起很大的延迟损失。大页面是以下问题的原因：</p>
</blockquote>

<ol>
<li>Fork is called, two processes with shared huge pages are created.
&gt; 调用<code>Fork</code>，创建两个共享大页面的进程。</li>
<li>In a busy instance, a few event loops runs will cause commands to target a few thousand of pages, causing the copy on write of almost the whole process memory.
&gt; 在繁忙的实例中，运行几个事件循环将导致命令以几千页为目标，从而导致在写入时复制几乎涉及到整个进程内存。</li>
<li>This will result in big latency and big memory usage.
&gt; 这会导致巨大延迟和巨大的内存占用。</li>
</ol>

<p>Make sure to <strong>disable transparent huge pages</strong> using the following command:</p>

<blockquote>
<p>确保要<strong>禁用透明大页</strong>，使用如下命令</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-typescript" data-lang="typescript"><span style="color:#000">echo</span> <span style="color:#000">never</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#a40000">/</span><span style="color:#a40000">s</span><span style="color:#a40000">y</span><span style="color:#a40000">s</span><span style="color:#a40000">/</span><span style="color:#a40000">k</span><span style="color:#a40000">e</span><span style="color:#a40000">r</span><span style="color:#a40000">n</span><span style="color:#a40000">e</span><span style="color:#a40000">l</span><span style="color:#a40000">/</span><span style="color:#a40000">m</span><span style="color:#a40000">m</span><span style="color:#a40000">/</span><span style="color:#a40000">t</span><span style="color:#a40000">r</span><span style="color:#a40000">a</span><span style="color:#a40000">n</span><span style="color:#a40000">s</span><span style="color:#a40000">p</span><span style="color:#a40000">a</span><span style="color:#a40000">r</span><span style="color:#a40000">e</span><span style="color:#a40000">n</span><span style="color:#a40000">t</span><span style="color:#a40000">_</span><span style="color:#a40000">h</span><span style="color:#a40000">u</span><span style="color:#a40000">g</span><span style="color:#a40000">e</span><span style="color:#a40000">p</span><span style="color:#a40000">a</span><span style="color:#a40000">g</span><span style="color:#a40000">e</span><span style="color:#a40000">/</span><span style="color:#a40000">e</span><span style="color:#a40000">n</span><span style="color:#a40000">a</span><span style="color:#a40000">b</span><span style="color:#a40000">l</span><span style="color:#a40000">e</span><span style="color:#a40000">d</span>
</code></pre></div>
<h2 id="latency-induced-by-swapping-operating-system-paging">Latency induced by swapping (operating system paging)</h2>

<blockquote>
<p>由 swapping(操作系统分页)产生的延迟</p>
</blockquote>

<p>Linux (and many other modern operating systems) is able to relocate memory pages from the memory to the disk, and vice versa, in order to use the system memory efficiently.</p>

<blockquote>
<p>Linux（和许多其他现代操作系统）能够将内存页从内存重新定位到磁盘，反之亦然，以便有效地使用系统内存。</p>
</blockquote>

<p>If a Redis page is moved by the kernel from the memory to the swap file, when the data stored in this memory page is used by Redis (for example accessing a key stored into this memory page) the kernel will stop the Redis process in order to move the page back into the main memory. This is a slow operation involving random I/Os (compared to accessing a page that is already in memory) and will result into anomalous latency experienced by Redis clients.</p>

<blockquote>
<p>如果内核将 Redis 页从内存移动到交换文件，则当 Redis 使用存储在此内存页中的数据（例如访问存储在此内存页中的密钥）时，内核将停止 Redis 进程，以便将该页移回主内存。这是一个涉及随机 I/O 的慢操作（与访问已在内存中的页面相比），并将导致 Redis 客户端遇到异常延迟。</p>
</blockquote>

<p>The kernel relocates Redis memory pages on disk mainly because of three reasons:</p>

<blockquote>
<p>内核重置Redis内在页到硬盘主要有以下三个原因：</p>
</blockquote>

<ul>
<li><p>The system is under memory pressure since the running processes are demanding more physical memory than the amount that is available. The simplest instance of this problem is simply Redis using more memory than is available.
&gt; 系统面临内存压力，因为正在运行的进程需要的物理内存比可用内存量多。此问题的最简单例子是 Redis 使用比可用内存更多的内存。</p></li>

<li><p>The Redis instance data set, or part of the data set, is mostly completely idle (never accessed by clients), so the kernel could swap idle memory pages on disk. This problem is very rare since even a moderately slow instance will touch all the memory pages often, forcing the kernel to retain all the pages in memory.
&gt; Redis 实例数据集或数据集的一部分，几乎是完全空闲的（客户端从不访问），因此内核可能会交换空闲内存页到磁盘。这个问题非常罕见，因为即使是中等速度较慢的实例也会经常接触所有内存页，迫使内核保留内存中的所有页。</p></li>

<li><p>Some processes are generating massive read or write I/Os on the system. Because files are generally cached, it tends to put pressure on the kernel to increase the filesystem cache, and therefore generate swapping activity. Please note it includes Redis RDB and/or AOF background threads which can produce large files.
&gt; 某些进程正在系统上生成大量读取或写入 I/O。由于文件通常是缓存的，因此它往往会给内核带来压力，以增加文件系统缓存，从而生成交换活动。请注意，它包括可以生成大文件的 Redis RDB 和/或 AOF 后台线程。</p></li>
</ul>

<p>Fortunately Linux offers good tools to investigate the problem, so the simplest thing to do is when latency due to swapping is suspected is just to check if this is the case.</p>

<blockquote>
<p>幸运的是，Linux提供了很好的工具来调查这个问题，所以最简单的办法是，当怀疑由于交换而导致的延迟时，只需检查是否属于这种情况。</p>
</blockquote>

<p>The first thing to do is to checking the amount of Redis memory that is swapped on disk. In order to do so you need to obtain the Redis instance pid:</p>

<blockquote>
<p>首先要做的是检查磁盘上交换的 Redis 内存量。为此，您需要获取 Redis 实例 pid：</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-makefile" data-lang="makefile"><span style="color:#a40000">$</span> <span style="color:#a40000">r</span><span style="color:#a40000">e</span><span style="color:#a40000">d</span><span style="color:#a40000">i</span><span style="color:#a40000">s</span><span style="color:#a40000">-</span><span style="color:#a40000">c</span><span style="color:#a40000">l</span><span style="color:#a40000">i</span> <span style="color:#a40000">i</span><span style="color:#a40000">n</span><span style="color:#a40000">f</span><span style="color:#a40000">o</span> <span style="color:#a40000">|</span> <span style="color:#a40000">g</span><span style="color:#a40000">r</span><span style="color:#a40000">e</span><span style="color:#a40000">p</span> <span style="color:#a40000">p</span><span style="color:#a40000">r</span><span style="color:#a40000">o</span><span style="color:#a40000">c</span><span style="color:#a40000">e</span><span style="color:#a40000">s</span><span style="color:#a40000">s</span><span style="color:#a40000">_</span><span style="color:#a40000">i</span><span style="color:#a40000">d</span>
<span style="color:#000">process_id</span><span style="color:#ce5c00;font-weight:bold">:</span>5454
</code></pre></div>
<p>Now enter the /proc file system directory for this process:</p>

<blockquote>
<p>然后进入这个进程的文件系统目录 /proc</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">$ <span style="color:#204a87">cd</span> /proc/5454</code></pre></div>
<p>Here you&rsquo;ll find a file called <strong>smaps</strong> that describes the memory layout of the Redis process (assuming you are using Linux 2.6.16 or newer). This file contains very detailed information about our process memory maps, and one field called <strong>Swap</strong> is exactly what we are looking for. However there is not just a single swap field since the smaps file contains the different memory maps of our Redis process (The memory layout of a process is more complex than a simple linear array of pages).</p>

<blockquote>
<p>在这里能找到一个名为<strong>smaps</strong>的文件，该文件描述了Redis进程的内存布局（假设您使用的是Linux 2.6.16或更高版本）。此文件包含有关进程内存映射的非常详细的信息，我们要找的是一个名为 <strong>Swap</strong> 的字段。但是，由于smaps文件包含Redis进程的不同内存映射（进程的内存布局比简单的线性页面数组更复杂），因此不仅仅是一个交换字段。</p>
</blockquote>

<p>Since we are interested in all the memory swapped by our process the first thing to do is to grep for the Swap field across all the file:</p>

<blockquote>
<p>因为我们要看所有的进程交换的内在，所以要从文件中grep出 Swap字段：</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-makefile" data-lang="makefile"><span style="color:#000">$ cat smaps | grep &#39;Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>&#39;
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                 12 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                156 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  8 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  4 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  4 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  4 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  4 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
<span style="color:#000">Swap</span><span style="color:#ce5c00;font-weight:bold">:</span>                  0 <span style="color:#000">kB</span>
</code></pre></div>
<p>If everything is 0 kB, or if there are sporadic 4k entries, everything is perfectly normal. Actually in our example instance (the one of a real web site running Redis and serving hundreds of users every second) there are a few entries that show more swapped pages. To investigate if this is a serious problem or not we change our command in order to also print the size of the memory map:</p>

<blockquote>
<p>如果全都是0kB，或者偶尔有4k的条目，说明没问题。实际上在我们的例子实例（真正并发几百用户的网站运行的Redis）有很多条目会展示更多交换页面。检查有没有大问题，我们需要修改命令来打印内存映射：</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-yaml" data-lang="yaml">$<span style="color:#f8f8f8;text-decoration:underline"> </span>cat<span style="color:#f8f8f8;text-decoration:underline"> </span>smaps<span style="color:#f8f8f8;text-decoration:underline"> </span>|<span style="color:#f8f8f8;text-decoration:underline"> </span>egrep<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#4e9a06">&#39;^(Swap|Size)&#39;</span><span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">               </span><span style="color:#0000cf;font-weight:bold">316</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                </span><span style="color:#0000cf;font-weight:bold">40</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">               </span><span style="color:#0000cf;font-weight:bold">132</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">            </span><span style="color:#0000cf;font-weight:bold">720896</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                </span><span style="color:#0000cf;font-weight:bold">12</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">              </span><span style="color:#0000cf;font-weight:bold">4096</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">               </span><span style="color:#0000cf;font-weight:bold">156</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">              </span><span style="color:#0000cf;font-weight:bold">4096</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">              </span><span style="color:#0000cf;font-weight:bold">4096</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">              </span><span style="color:#0000cf;font-weight:bold">1272</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                </span><span style="color:#0000cf;font-weight:bold">16</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                </span><span style="color:#0000cf;font-weight:bold">84</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">               </span><span style="color:#0000cf;font-weight:bold">144</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                </span><span style="color:#0000cf;font-weight:bold">12</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">               </span><span style="color:#0000cf;font-weight:bold">108</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">               </span><span style="color:#0000cf;font-weight:bold">272</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Size</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB<span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">Swap</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#f8f8f8;text-decoration:underline">                 </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span>kB</code></pre></div>
<p>As you can see from the output, there is a map of 720896 kB (with just 12 kB swapped) and 156 kB more swapped in another map: basically a very small amount of our memory is swapped so this is not going to create any problem at all.</p>

<blockquote>
<p>从上面的输出可以看到，有一条映射是 720896kB(只有12kB的交换)和 另外一条有超过156 kB交换的映射：基本上一个很少量的内在交换没啥大问题。</p>
</blockquote>

<p>If instead a non trivial amount of the process memory is swapped on disk your latency problems are likely related to swapping. If this is the case with your Redis instance you can further verify it using the <strong>vmstat</strong> command:</p>

<blockquote>
<p>相反，如果在磁盘上交换了大量进程内存，则延迟问题可能与交换有关。如果您的 Redis 实例属于这种情况，您可以使用 <strong>vmstat</strong> 命令进一步验证它：</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-yaml" data-lang="yaml">$<span style="color:#f8f8f8;text-decoration:underline"> </span>vmstat<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span>procs<span style="color:#f8f8f8;text-decoration:underline"> </span>-----------memory---------- ---swap-- -----io---- -system-- ----cpu---<span style="color:#8f5902;font-style:italic">-
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic"> </span><span style="color:#8f5902;font-style:italic">r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa</span><span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">   </span><span style="color:#0000cf;font-weight:bold">3980</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">697932</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">147180</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1406456</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">     </span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#f8f8f8;text-decoration:underline">     </span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">91</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">   </span><span style="color:#0000cf;font-weight:bold">3980</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">697428</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">147180</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1406580</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">     </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">     </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">19088</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">16104</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">9</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">6</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">84</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">   </span><span style="color:#0000cf;font-weight:bold">3980</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">697296</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">147180</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1406616</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">     </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">28</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">18936</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">16193</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">7</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">6</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">87</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">   </span><span style="color:#0000cf;font-weight:bold">3980</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">697048</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">147180</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1406640</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">     </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">     </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">18613</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">15987</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">6</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">6</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">88</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">   </span><span style="color:#0000cf;font-weight:bold">3980</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">696924</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">147180</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1406656</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">     </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">     </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">18744</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">16299</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">6</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">5</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">88</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">   </span><span style="color:#0000cf;font-weight:bold">3980</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">697048</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">147180</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1406688</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">    </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">     </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">     </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">18520</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">15974</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">6</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">6</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">88</span><span style="color:#f8f8f8;text-decoration:underline">  </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#f8f8f8;text-decoration:underline">
</span><span style="color:#f8f8f8;text-decoration:underline"></span>^C</code></pre></div>
<p>The interesting part of the output for our needs are the two columns <strong>si</strong> and <strong>so</strong>, that counts the amount of memory swapped from/to the swap file. If you see non zero counts in those two columns then there is swapping activity in your system.</p>

<blockquote>
<p>我们需要的部分是 <strong>si</strong>和<strong>so</strong>两个字段，这记录了内存从/向交换文件交换的内存的量。如果你在这两个字段中看到非0的记录，说明在你的系统中有交换活动。</p>
</blockquote>

<p>Finally, the <strong>iostat</strong> command can be used to check the global I/O activity of the system.</p>

<p>最后，<strong>isstat</strong> 命令可以且来检查系统全局的 I/O活动。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">$ iostat -xk <span style="color:#0000cf;font-weight:bold">1</span>
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          13.55    0.04    2.92    0.53    0.00   82.95

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.77     0.00    0.01    0.00     0.40     0.00    73.65     0.00    3.62   2.58   0.00
sdb               1.27     4.75    0.82    3.54    38.00    32.32    32.19     0.11   24.80   4.24   1.85</code></pre></div>
<p>If your latency problem is due to Redis memory being swapped on disk you need to lower the memory pressure in your system, either adding more RAM if Redis is using more memory than the available, or avoiding running other memory hungry processes in the same system.</p>

<blockquote>
<p>如果延迟问题是由于在磁盘上交换 Redis 内存而导致的，则需要降低系统中的内存压力，如果 Redis 使用的内存多于可用内存，则添加更多 RAM，或者避免在同一系统中运行其他内存密集型进程。</p>
</blockquote>

<h2 id="latency-due-to-aof-and-disk-i-o">Latency due to AOF and disk I/O</h2>

<blockquote>
<p>由AOF和disk I/O导致的延迟</p>
</blockquote>

<p>Another source of latency is due to the Append Only File support on Redis. The AOF basically uses two system calls to accomplish its work. One is write(2) that is used in order to write data to the append only file, and the other one is fdatasync(2) that is used in order to flush the kernel file buffer on disk in order to ensure the durability level specified by the user.</p>

<blockquote>
<p>另一个延迟来源是由于 Redis的AOF支持。AOF基本使用两个系统调用来实现。一个是write(2) ，用于将数据写到 aof文件中，另一个命令是 fdatasync(2)，用于刷新磁盘上的内核文件缓冲区，确保用户指定的持久性级别。</p>
</blockquote>

<p>Both the write(2) and fdatasync(2) calls can be source of latency. For instance write(2) can block both when there is a system wide sync in progress, or when the output buffers are full and the kernel requires to flush on disk in order to accept new writes.</p>

<blockquote>
<p>write(2) 和 fdatasync(2) 两个调用都有可能成为延迟的来源。例如write(2)可能在进程有系统范围的同步时阻塞，也可能在输出缓冲区满了并且内核为了接收新的写入需要刷新硬盘上的输出缓冲区时造成阻塞。</p>
</blockquote>

<p>The fdatasync(2) call is a worse source of latency as with many combinations of kernels and file systems used it can take from a few milliseconds to a few seconds to complete, especially in the case of some other process doing I/O. For this reason when possible Redis does the fdatasync(2) call in a different thread since Redis 2.4.</p>

<blockquote>
<p>fdatasync(2)调用是更糟的延迟来源，因为内核和文件系统有很多关联，调用 fdatasync(2)会花费几毫秒到几秒来完成，特别是在其他进程进行I/O的情况下。因此，Redis2.4以后会在其他线程中调用  fdatasync(2) 方法。</p>
</blockquote>

<p>We&rsquo;ll see how configuration can affect the amount and source of latency when using the AOF file.</p>

<blockquote>
<p>我们看看使用AOF文件配置文件如何对延迟的来源和值造成影响。</p>
</blockquote>

<p>The AOF can be configured to perform a fsync on disk in three different ways using the <strong>appendfsync</strong> configuration option (this setting can be modified at runtime using the <strong>CONFIG SET</strong> command).</p>

<blockquote>
<p>AOF配置刷盘有三种 <strong>appendfsync</strong> 配置选项。（可以通过<strong>CONFIG SET</strong> 命令在运行时动态设置）</p>
</blockquote>

<ul>
<li><p>When appendfsync is set to the value of <strong>no</strong> Redis performs no fsync. In this configuration the only source of latency can be write(2). When this happens usually there is no solution since simply the disk can&rsquo;t cope with the speed at which Redis is receiving data, however this is uncommon if the disk is not seriously slowed down by other processes doing I/O.
&gt; 当 appendfsync 值设置为<strong>no</strong> Redis 不会执行fsync。这个配置下延迟的唯一来源是write(2)。当发生时，通常是没法解决的，因为磁盘没法承受Redis接收数据的速度，但是如果磁盘没有被执行I / O的其他进程严重减慢，则这种情况并不常见。</p></li>

<li><p>When appendfsync is set to the value of <strong>everysec</strong> Redis performs a fsync every second. It uses a different thread, and if the fsync is still in progress Redis uses a buffer to delay the write(2) call up to two seconds (since write would block on Linux if a fsync is in progress against the same file). However if the fsync is taking too long Redis will eventually perform the write(2) call even if the fsync is still in progress, and this can be a source of latency.
&gt; 当appendfsync追加同步值设置为 <strong>everysec</strong>每秒时，Redis会每秒执行一次 fsync同步。它会使用一个不同的线程，并且如果fsync正在进行中，Redis会使用一个缓冲区来将 write(2)调用延迟2秒（因为Linux上write操作会被阻塞，如果一个fsync同步正在对相同文件进行操作）。然而如果fsync同步消耗太长时间，Redis仍会执行write(2)调用即使fsync仍在进行中，这可能是延迟来源之一。</p></li>

<li><p>When appendfsync is set to the value of <strong>always</strong> a fsync is performed at every write operation before replying back to the client with an OK code (actually Redis will try to cluster many commands executed at the same time into a single fsync). In this mode performances are very low in general and it is strongly recommended to use a fast disk and a file system implementation that can perform the fsync in short time.
&gt; 当appendfsync追加同步值设置为<strong>always</strong>，每个write操作在给客户端返回OK编码之前都会执行一次fsync同步（实际上 Redis将尝试聚集同时执行的多个命令集中到一次fsync同步中）。在这种模式下，性能通常会非常低，并且非常推荐使用一个高速磁盘和文件系统实现来在短时间内执行fsync同步。</p></li>
</ul>

<p>Most Redis users will use either the <strong>no</strong> or <strong>everysec</strong> setting for the appendfsync configuration directive. The suggestion for minimum latency is to avoid other processes doing I/O in the same system. Using an SSD disk can help as well, but usually even non SSD disks perform well with the append only file if the disk is spare as Redis writes to the append only file without performing any seek.</p>

<blockquote>
<p>多数 Redis用户会对appendfsync追加同步配置使用 <strong>no</strong> 或者 <strong>everysec</strong>设置。最小延迟的建议是避免同一系统中其他进程在执行 I/O 操作。使用SSD硬盘也有用，但通常如果硬盘是备用盘，并且Redis仅写入追加文件不执行任何搜索的情况下，非SSD硬盘的表现也很好，</p>
</blockquote>

<p>If you want to investigate your latency issues related to the append only file you can use the strace command under Linux:</p>

<p>如果你想要排查仅追加文件相关的延迟问题，你可以使用Linux系统的追踪命令：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-javascript" data-lang="javascript"><span style="color:#000">sudo</span> <span style="color:#000">strace</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">p</span> <span style="color:#000">$</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">pidof</span> <span style="color:#000">redis</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">server</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">T</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">e</span> <span style="color:#000">trace</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">fdatasync</span>
</code></pre></div>
<p>The above command will show all the fdatasync(2) system calls performed by Redis in the main thread. With the above command you&rsquo;ll not see the fdatasync system calls performed by the background thread when the appendfsync config option is set to <strong>everysec</strong>. In order to do so just add the -f switch to strace.</p>

<blockquote>
<p>以上命令将会展示所有Redis主线程执行的的fdatasync(2)系统调用。使用以上命令无法查探到追加配置为<strong>everysec</strong>时后台线程执行的 fdatasync系统调用的记录。如果想查看到，需要加 -f 参数切换到相应追踪。</p>
</blockquote>

<p>If you wish you can also see both fdatasync and write system calls with the following command:</p>

<blockquote>
<p>如果你希望同时查看到 fdatasync和write系统调用，可以使用如下命令：</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-javascript" data-lang="javascript"><span style="color:#000">sudo</span> <span style="color:#000">strace</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">p</span> <span style="color:#000">$</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">pidof</span> <span style="color:#000">redis</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">server</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">T</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">e</span> <span style="color:#000">trace</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">fdatasync</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">write</span>
</code></pre></div>
<p>However since write(2) is also used in order to write data to the client sockets this will likely show too many things unrelated to disk I/O. Apparently there is no way to tell strace to just show slow system calls so I use the following command:</p>

<blockquote>
<p>由于 write(2)也会被用于写数据到客户端套接字，执行这个命令可能会展示过多与硬盘I/O无关的记录。可以用如下命令只展示慢的系统调用 。</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-perl" data-lang="perl"><span style="color:#000"></span><span style="color:#000">sudo</span> <span style="color:#000"></span><span style="color:#000">strace</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000"></span><span style="color:#000">f</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000"></span><span style="color:#000">p</span> <span style="color:#000">$</span><span style="color:#a40000">(</span><span style="color:#000">pidof</span> <span style="color:#000"></span><span style="color:#000">redis</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000"></span><span style="color:#000">server</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000"></span><span style="color:#000">T</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000"></span><span style="color:#000">e</span> <span style="color:#000"></span><span style="color:#000">trace</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000"></span><span style="color:#000">fdatasync</span><span style="color:#000;font-weight:bold">,</span><span style="color:#204a87">write</span> <span style="color:#0000cf;font-weight:bold">2</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#0000cf;font-weight:bold">1</span> <span style="color:#ce5c00;font-weight:bold">|</span> <span style="color:#204a87">grep</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000"></span><span style="color:#000">v</span> <span style="color:#4e9a06">&#39;0.0&#39;</span> <span style="color:#ce5c00;font-weight:bold">|</span> <span style="color:#204a87">grep</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000"></span><span style="color:#000">v</span> <span style="color:#000"></span><span style="color:#000">unfinished</span></code></pre></div>
<h2 id="latency-generated-by-expires">Latency generated by expires</h2>

<blockquote>
<p>因失效产生的延迟</p>
</blockquote>

<p>Redis evict expired keys in two ways:</p>

<blockquote>
<p>Redis以以下两种方式淘汰过期key：</p>
</blockquote>

<ul>
<li>One <em>lazy</em> way expires a key when it is requested by a command, but it is found to be already expired.
&gt; 一个*懒惰*的方式，当一个key被命令请求到但发现这个key已经过期的时候才淘汰key。</li>
<li>One <em>active</em> way expires a few keys every 100 milliseconds.
&gt; 一个*主动*方式每100毫秒淘汰一些key。</li>
</ul>

<p>The active expiring is designed to be adaptive. An expire cycle is started every 100 milliseconds (10 times per second), and will do the following:</p>

<blockquote>
<p>主动过期旨在具有自适应性。每 100 毫秒（每秒 10 次）启动一个过期周期，并将执行以下操作：</p>
</blockquote>

<ul>
<li>Sample <code>ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP</code> keys, evicting all the keys already expired.
&gt; 采样 <code>ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP</code>个数量的key，淘汰所有的已经过期的key。</li>
<li>If the more than 25% of the keys were found expired, repeat.
&gt; 如果超过25%的key被发现过期了，重复。</li>
</ul>

<p>Given that <code>ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP</code> is set to 20 by default, and the process is performed ten times per second, usually just 200 keys per second are actively expired. This is enough to clean the DB fast enough even when already expired keys are not accessed for a long time, so that the <em>lazy</em> algorithm does not help. At the same time expiring just 200 keys per second has no effects in the latency a Redis instance.</p>

<blockquote>
<p><code>ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP</code>默认设置为20，这个处理每秒钟执行10次，通常每秒只有200个key被主动过期淘汰。这足以足够快地清理数据库，即使已经过期的key长时间未被访问，因此 <em>lazy</em> 算法无济于事。同时，每秒仅过期 200 个key不会造成 Redis 实例的延迟。</p>
</blockquote>

<p>However the algorithm is adaptive and will loop if it finds more than 25% of keys already expired in the set of sampled keys. But given that we run the algorithm ten times per second, this means that the unlucky event of more than 25% of the keys in our random sample are expiring at least <em>in the same second</em>.</p>

<blockquote>
<p>然而算法是适应性的，如果发现采样范围的key超过25%的key已经过期的话会循环。但是，假设我们每秒运行该算法十次，这意味着随机样本中超过25%的key的不幸事件至少*在同一秒*到期。</p>
</blockquote>

<p>Basically this means that <strong>if the database has many, many keys expiring in the same second, and these make up at least 25% of the current population of keys with an expire set</strong>, Redis can block in order to get the percentage of keys already expired below 25%.</p>

<blockquote>
<p>基本上，这意味着<strong>如果数据库有许多很多key在同一秒内过期，并且这些过期的key占总数的至少25%</strong>，Redis会阻塞将过期key的比例降低至低于25%。</p>
</blockquote>

<p>This approach is needed in order to avoid using too much memory for keys that are already expired, and usually is absolutely harmless since it&rsquo;s strange that a big number of keys are going to expire in the same exact second, but it is not impossible that the user used <a href="https://redis.io/commands/expireat"><code>EXPIREAT</code></a> extensively with the same Unix time.</p>

<blockquote>
<p>为了避免过期的key使用过多的内存，这个方法很有必要，并且通常是完全无害的，因为大量的key同时在精确的1秒过期很奇怪，虽然使用<a href="https://redis.io/commands/expireat"><code>EXPIREAT</code></a> 也不是不能实现。</p>
</blockquote>

<p>In short: be aware that many keys expiring at the same moment can be a source of latency.</p>

<blockquote>
<p>总结，要注意大量key在同一秒过期可能会是延迟的来源之一。</p>
</blockquote>

<h2 id="redis-software-watchdog">Redis software watchdog</h2>

<blockquote>
<p>Redis软件监视者</p>
</blockquote>

<p>Redis 2.6 introduces the <em>Redis Software Watchdog</em> that is a debugging tool designed to track those latency problems that for one reason or the other escaped an analysis using normal tools.</p>

<blockquote>
<p>Redis 2.6引入了*Redis软件看门狗*，这是一个调试工具，旨在跟踪那些由于某种原因而无法使用普通工具进行分析的延迟问题。</p>
</blockquote>

<p>The software watchdog is an experimental feature. While it is designed to be used in production environments care should be taken to backup the database before proceeding as it could possibly have unexpected interactions with the normal execution of the Redis server.</p>

<blockquote>
<p>软件看门狗是一个实验特性。虽然它设计用于生产环境，但在继续操作之前，应注意备份数据库，因为它可能会与Redis服务器的正常执行发生意外交互。</p>
</blockquote>

<p>It is important to use it only as <em>last resort</em> when there is no way to track the issue by other means.</p>

<blockquote>
<p>重要的是，只有在无法通过其他方式跟踪问题时，才能将其用作“最后手段”。</p>
</blockquote>

<p>This is how this feature works:</p>

<blockquote>
<p>以下是如何使用该特性：</p>
</blockquote>

<ul>
<li><p>The user enables the software watchdog using the <a href="https://redis.io/commands/config-set"><code>CONFIG SET</code></a> command.
&gt; 用户通过 <code>CONFIG SET</code>命令开启软件看门狗功能。</p></li>

<li><p>Redis starts monitoring itself constantly.
&gt; Redis开始持续监控自身。</p></li>

<li><p>If Redis detects that the server is blocked into some operation that is not returning fast enough, and that may be the source of the latency issue, a low level report about where the server is blocked is dumped on the log file.
&gt; 如果Redis检测到Redis服务被某些返回不够快的操作阻塞，这些可能是延迟的来源，一个关于服务在哪被阻塞的低等级的报告将被输出到日志文件中。</p></li>

<li><p>The user contacts the developers writing a message in the Redis Google Group, including the watchdog report in the message.
&gt; 用户将带有看门狗报告的信息的消息发送给Redis Google Group。</p></li>
</ul>

<p>Note that this feature cannot be enabled using the redis.conf file, because it is designed to be enabled only in already running instances and only for debugging purposes.</p>

<blockquote>
<p>要注意这个我并没有在redis.conf配置文件中开启，因为它被设计为仅在已经运行的实例中以调试的目的进行开启。</p>
</blockquote>

<p>To enable the feature just use the following:</p>

<blockquote>
<p>开启该我只需要使用如下命令：</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000">CONFIG</span> <span style="color:#204a87;font-weight:bold">SET</span> <span style="color:#000">watchdog</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">period</span> <span style="color:#0000cf;font-weight:bold">500</span></code></pre></div>
<p>The period is specified in milliseconds. In the above example I specified to log latency issues only if the server detects a delay of 500 milliseconds or greater. The minimum configurable period is 200 milliseconds.</p>

<blockquote>
<p>以毫秒指定周期。在以上的例子我定义仅当服务器检测到超过500毫秒及以上的延迟才记录延迟问题。这个周期最小为200毫秒。</p>
</blockquote>

<p>When you are done with the software watchdog you can turn it off setting the <code>watchdog-period</code> parameter to 0. <strong>Important:</strong> remember to do this because keeping the instance with the watchdog turned on for a longer time than needed is generally not a good idea.</p>

<blockquote>
<p>软件看门狗可以通过设置 <code>watchdog-period</code>值为0进行关闭。<strong>重点：</strong>用完就关闭是好习惯。</p>
</blockquote>

<p>The following is an example of what you&rsquo;ll see printed in the log file once the software watchdog detects a delay longer than the configured one:</p>

<blockquote>
<p>以下是软件看门狗检测到延迟时间超过配置的日志输出的一个例子：</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-css" data-lang="css"><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">8547</span> <span style="color:#ce5c00;font-weight:bold">|</span> <span style="color:#204a87;font-weight:bold">signal</span> <span style="color:#204a87;font-weight:bold">handler</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">1333114359</span><span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#204a87;font-weight:bold">---</span> <span style="color:#204a87;font-weight:bold">WATCHDOG</span> <span style="color:#204a87;font-weight:bold">TIMER</span> <span style="color:#204a87;font-weight:bold">EXPIRED</span> <span style="color:#204a87;font-weight:bold">---</span>
<span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">lib</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">libc</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">so</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">6</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">nanosleep</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0x2d</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x7f16b5c2d39d</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">lib</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">libpthread</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">so</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0xf8f0</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x7f16b5f158f0</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">lib</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">libc</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">so</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">6</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">nanosleep</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0x2d</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x7f16b5c2d39d</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">lib</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">libc</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">so</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">6</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">usleep</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0x34</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x7f16b5c62844</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">redis-server</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">debugCommand</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0x3e1</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x43ab41</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">redis-server</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">call</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0x5d</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x415a9d</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">redis-server</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">processCommand</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0x375</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x415fc5</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">redis-server</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">processInputBuffer</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0x4f</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x4203cf</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">redis-server</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">readQueryFromClient</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0xa0</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x4204e0</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">redis-server</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">aeProcessEvents</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0x128</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x411b48</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">redis-server</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">aeMain</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0x2b</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x411dbb</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">redis-server</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">main</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0x2b6</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x418556</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">lib</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">libc</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">so</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">6</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">__libc_start_main</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">0xfd</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x7f16b5ba1c4d</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#204a87;font-weight:bold">redis-server</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0x411099</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#204a87;font-weight:bold">------</span></code></pre></div>
<p>Note: in the example the <strong>DEBUG SLEEP</strong> command was used in order to block the server. The stack trace is different if the server blocks in a different context.</p>

<blockquote>
<p>说明：案例中 <strong>DEBUG SLEEP</strong> 命令用于阻塞服务。其他情况的输出会不一样。</p>
</blockquote>

<p>If you happen to collect multiple watchdog stack traces you are encouraged to send everything to the Redis Google Group: the more traces we obtain, the simpler it will be to understand what the problem with your instance is.</p>

<blockquote>
<p>如果你收集到多种看门狗栈信息，你最好把所有的信息都发送给Redis Google Group：我去提供的信息越详细，别人越容易理解你的实例出了什么问题。</p>
</blockquote>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis%E5%AE%9E%E4%BE%8B%E5%AE%95%E6%9C%BA%E6%97%A5%E5%BF%97%E5%88%86%E6%9E%90/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E9%AA%9A%E6%93%8D%E4%BD%9C%E7%BB%91%E6%A0%B8/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>